﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using JilyHelper;
using GameControler.Emgu;
using Helper;

namespace GameControler.Game
{

    public class TemplateView
    {
        private static List<string> extensions = new List<string>() { ".png", ".jpg", ".bmp" };
        public static List<string> Extensions
        {
            get { return extensions; }
            set
            {
                extensions = value;
            }
        }

        public Bitmap Image { get; set; }
        public Point Point { get; set; }
        public string Name { get; set; }
        public string Team { get; set; }

        public static TemplateView CreatTemplateView(string filepath)
        {
            var template = new TemplateView();
            var fileinfo = new FileInfo(filepath);
            if (fileinfo.Exists && Extensions.Any(i => fileinfo.Extension.Equals(i, StringComparison.InvariantCultureIgnoreCase)))
            {
                var name = fileinfo.Name.Replace(fileinfo.Extension, "");
                if (!string.IsNullOrWhiteSpace(name) && name.Contains('#'))
                {
                    var ps = name.Split('#');
                    if (ps != null)
                    {
                        double x = 0;
                        double y = 0;
                        bool cancreat = false;
                        if (ps.Count() == 2)
                        {
                            cancreat = (double.TryParse(new string(ps[0].ToArray()), out x) && double.TryParse(new string(ps[1].ToArray()), out y));
                        }
                        else if (ps.Count() == 3)
                        {
                            cancreat = (double.TryParse(new string(ps[1].ToArray()), out x) && double.TryParse(new string(ps[2].ToArray()), out y));
                            if (cancreat)
                            {
                                template.Team = ps[0];
                            }
                        }
                        if (cancreat)
                        {
                            template.Point = new Point((int)x, (int)y);
                            template.Name = name;
                            template.Image = new Bitmap(fileinfo.FullName);
                            return template;
                        }
                    }
                }
            }
            return null;
        }
    }

    public abstract class ViewObject
    {
        public abstract string FloderName { get; }

        public string FloderPath
        {
            get
            {
                return Path.Combine(GameHelper.MainFloder, FloderName);
            }
        }

        public List<TemplateView> Templates = new List<TemplateView>();

        protected bool IsInitTemplate = false;

        protected ViewObject()
        {
            if (Templates.Count == 0)
            {
                InitTemplates();
            }
        }

        protected virtual void InitTemplates()
        {
            if (Directory.Exists(FloderPath))
            {
                var files =  DirectoryHelper.GetAllFiles(FloderPath);
                foreach (var item in files)
                {
                    var t = TemplateView.CreatTemplateView(item.FullName);
                    if (t != null)
                    {
                        Templates.Add(t);
                    }
                }
                IsInitTemplate = true;
            }
        }

        public virtual bool IsCurrentView(Bitmap intputbmp)
        {
            if (IsInitTemplate)
            {
                var iscurrent = Templates.Count > 0 && Templates.All(i => JilyEmgu.MatchResult(intputbmp, i).IsMatch);
                return iscurrent;
            }
            return false;
        }

    }
}
